home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 9 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.1 KB  |  122 lines

  1. Path: ix.netcom.com!netnews
  2. From: cpmorris@popd.ix.netcom.com (Chris Morris)
  3. Newsgroups: comp.lang.c++
  4. Subject: Friends....
  5. Date: Tue, 02 Jan 1996 00:08:12 GMT
  6. Organization: Netcom
  7. Message-ID: <4c9srh$g2l@ixnews3.ix.netcom.com>
  8. NNTP-Posting-Host: ix-stp-fl4-10.ix.netcom.com
  9. X-NETCOM-Date: Mon Jan 01  4:05:05 PM PST 1996
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12.     I am a beginner with C++ and have a question concerning the
  13. declaration of 'friend' functions.  I am working with Windows
  14. programming, but this question doesn't apply to Windows, it is a C++
  15. question.
  16.  
  17.     This is the class code I am confused about
  18.  
  19.  
  20. class TLine : public TPoints {
  21.   public:
  22.     TLine(int penSize = 1) : TPoints(10, 0, 10)
  23.     {
  24.       PenSize = penSize;
  25.     }
  26.  
  27.     int QueryPen() const
  28.     {
  29.       return PenSize;
  30.     }
  31.  
  32.     int QueryPen(int penSize);
  33.  
  34.     // The == operator must be defined for the container class, even
  35.     if unused
  36.     bool operator ==(const TLine& other) const
  37.     {
  38.       return &other == this;
  39.     }
  40.  
  41.     friend ostream& operator <<(ostream& os, const TLine& line);
  42.     friend istream& operator >>(istream& is, TLine& line);
  43.  
  44.   protected:
  45.     int PenSize;
  46. };
  47.  
  48.  
  49.     Why are the two operator functions (<< and >>) declared as friends?
  50. Is it because the original declarations are not virtual and cannot be
  51. overridden?  I understand that these function have to be modified
  52. because they deal with a user-defined type.  But look at the
  53. definitions of the functions
  54.  
  55.  
  56. ostream&
  57. operator <<(ostream& os, const TLine& line)
  58. {
  59.   // Write the number of points in the line
  60.   os << line.GetItemsInContainer();
  61.  
  62.   // Write the pen size
  63.   os << ' ' << line.PenSize;
  64.  
  65.   // Get an iterator for the array of points
  66.   TPointsIterator j(line);
  67.  
  68.   // While the iterator is valid (i.e. we haven't run out of points)
  69.   while(j)
  70.     // Write the point from the iterator and increment the array.
  71.     os << j++;
  72.  
  73.   os << '\n';
  74.   // return the stream object
  75.   return os;
  76. }
  77.  
  78. istream&
  79. operator >>(istream& is, TLine& line)
  80. {
  81.   unsigned numPoints;
  82.  
  83.   is >> numPoints;
  84.  
  85.   is >> line.PenSize;
  86.  
  87.   while (numPoints--) {
  88.     TPoint point;
  89.     is >> point;
  90.     line.Add(point);
  91.   }
  92.   // return the stream object
  93.   return is;
  94. }
  95.  
  96.  
  97.     This is the confusing part.  How can the << and >> be used in the
  98. definitions of << and >>?  Is seems like the new << and >> friend
  99. functions are just defining their own implementations for handling the
  100. user-defined type.  This is overloading, isn't it?
  101.  
  102.     I have, and am nearly finished with "The C++ Programming Language" by
  103. Bjarne Stroustrup.  When I look at the friend function section he says
  104. that friends are used so that two classes can have functions in
  105. common.  The function is able to access the private part of each
  106. class.  But he says nothing about using a 'friend' declaration to do
  107. what this example is doing.  Help?
  108.  
  109.     Thanks in advance...
  110.  
  111.  
  112. -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  113.  
  114.                Chris Morris  (cpmorris@ix.netcom.com)
  115.                P. O. Box 574                       
  116.                Clearwater, FL  34617-0574
  117.  
  118.  
  119. Reality is a crutch for people who can't deal with science fiction . . .
  120. -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  121.  
  122.